package com.esm.sm800.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.esm.sm800.R;
import com.esm.sm800.database.dbmodel.Buzhen_report;
import com.esm.sm800.database.dbmodel.Hiskeyrecord;
import com.esm.sm800.database.dbmodel.Member;
import com.esm.sm800.database.manager.BuzhenreportCenter;
import com.esm.sm800.database.manager.HiskeyrecordCenter;
import com.esm.sm800.database.manager.MemberCenter;
import com.esm.sm800.database.manager.NeedleCenter;
import com.esm.sm800.database.manager.SystemCenter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.BottomMenuWindow;

//补针数量编辑
public class BzsbjActivity extends BaseActivity implements View.OnClickListener {

    private final String TAG = "BzsbjActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_bzsbj);

        tv_cl = (TextView) findViewById(R.id.tv_cl);
        tv_title = (TextView) findViewById(R.id.tv_title);
        et_num = (EditText) findViewById(R.id.et_num);
        findViewById(R.id.tv_ok).setOnClickListener(this);
        findViewById(R.id.tv_cancel).setOnClickListener(this);
        if (SystemCenter.getInstance().needleEdit == null) {
            finish();
            return;
        }
        tv_cl.setText(SystemCenter.getInstance().needleEdit.getStock() + "");
        tv_title.setText(getString(R.string.bzslbj) + " " + SystemCenter.getInstance().needleEdit.getName());
    }

    private TextView tv_cl;
    private TextView tv_title;
    private EditText et_num;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_ok:
                onSave();
                break;
            case R.id.tv_cancel:
                finish();
                break;
        }
    }

    private void onSave() {
        String strnum = et_num.getText().toString();
        if (strnum.length() < 1) {
            showShortToast("请输入存量！");
            return;
        }

        //获取当前日期
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date();
        String strDate = formatter.format(curDate);
        //

        try {
            int num = Integer.parseInt(strnum);
            int num0 = SystemCenter.getInstance().needleEdit.getStock();
            int xNum = num - num0;//增加的数量
            SystemCenter.getInstance().needleEdit.setStock(num);
            NeedleCenter.getInstance().saveNeedle(SystemCenter.getInstance().needleEdit);

            Buzhen_report buzhen_report = new Buzhen_report();
            buzhen_report.setCreat_time(strDate);
            buzhen_report.setUid(SystemCenter.getInstance().member.getId());
            buzhen_report.setMname(SystemCenter.getInstance().member.getName());
            buzhen_report.setNeedle_name(SystemCenter.getInstance().needleEdit.getName());
            buzhen_report.setPosition(SystemCenter.getInstance().needleEdit.getPosition());
            buzhen_report.setReturn_time(strDate);
            buzhen_report.setNum(xNum);
            BuzhenreportCenter.getInstance().saveBuzhen_report(buzhen_report);

        } catch (Exception e) {
            Toast.makeText(BzsbjActivity.this, "错误的数字！", Toast.LENGTH_SHORT).show();
            return;
        }

        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {

    }
}

